package com.controllers;

import com.alibaba.fastjson.JSONObject;
import com.controllers.forms.LoginForm;
import com.controllers.forms.SimpleResponse;
import com.controllers.forms.user.self.CurrentUserInfo;
import com.mapper.UserMapper;
import com.models.User;
import com.services.interfaces.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/4/12.
 */
@Controller
public class SiteController extends BaseController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = {"/view/**/*", "/view/"}, method = {RequestMethod.GET})
    public Object page_build(HttpServletRequest request) {
        User user = currentUser(request);
        if (user == null) {
            return "redirect:/view/login";
        }
        return "layout_build";
    }



    @RequestMapping(value = {"/view2/**/*", "/view2/"}, method = {RequestMethod.GET})
    public Object page_dev(HttpServletRequest request) {
        System.out.println(this.userMapper.findByName("shao"));
//        loginAsUser(request, this.entityFinderService.uniqueEqColumn(User.class, "username", "root"));
        User user = currentUser(request);
        if (user == null) {
            return "redirect:/view2/login";
        }
        return "layout_dev";
    }


    @RequestMapping(value = {"/view2/login"}, method = {RequestMethod.GET})
    public Object page_login_dev(HttpServletRequest request) {
        return "layout_dev";
    }

    @RequestMapping(value = {"/view/login"}, method = {RequestMethod.GET})
    public Object page_login_build(HttpServletRequest request) {
        return "layout_build";
    }

    @RequestMapping(value = {"/", "/index"}, method = {RequestMethod.GET})
    public Object index(HttpServletRequest request) {
        return "redirect:/view/console/index";
    }

    @RequestMapping(value = {"/view/chart"}, method = {RequestMethod.GET})
    public Object notNeedLogin(HttpServletRequest request) {
        return "layout_build";
    }

    @RequestMapping(value = {"/api/site/login"}, method = {RequestMethod.POST})
    public
    @ResponseBody
    JSONObject login(HttpServletRequest request, @RequestBody LoginForm form) {
//        User user = this.entityFinderService.uniqueEqColumn(User.class, "username",form.getUsername());
//        if (user == null || !userService.matchUserPassword(user, form.getPassword())) {
//            return SimpleResponse.fail("用户名或者密码不正确");
//        }
//        if(user.getState().equals(User.State.stop)){
//            return SimpleResponse.fail("此用户无法登录");
//        }
//        loginAsUser(request, user);
//
//
//        CurrentUserInfo currentUserInfo = new CurrentUserInfo();
//        currentUserInfo.setUser(user);
//        currentUserInfo.setPermissionList(getCurrentUserPermissionStr(request));
//        return SimpleResponse.ok(currentUserInfo);
        return SimpleResponse.ok(null);
    }


    @RequestMapping(value = {"/api/site/logout"}, method = {RequestMethod.POST})
    public
    @ResponseBody
    JSONObject logout(HttpServletRequest request) {
        clearLoginedUser(request);
        return SimpleResponse.ok("注销成功");
    }
}
